﻿Imports System.Security.Cryptography
Imports System.Text
Public Class HamHoTro

    Public Shared Function MaHoaMD5(ByVal str As String) As String
        Dim kq As String = ""
        Dim md5Hasher As New MD5CryptoServiceProvider
        Dim hasherBytes As Byte()
        Dim encoder As New UTF8Encoding
        hasherBytes = md5Hasher.ComputeHash(encoder.GetBytes(str))
        kq = BitConverter.ToString(hasherBytes).Replace("-", "").ToLower()
        Return kq
    End Function

    Public Shared Function MaHoaMD5x2(ByVal str As String) As String
        Dim kq As String = ""
        Dim str2 As String = ""
        str2 = MaHoaMD5(str)
        kq = MaHoaMD5(str2)
        Return kq
    End Function

    Public Shared Function KiemTraRong(ByVal str As String, ByVal name As String) As String
        Dim err As String = ""

        If str.Trim().Length = 0 Then
            err += name + " không được để trống." + ControlChars.NewLine
        End If

        Return err
    End Function

    Public Shared Function DocThongSo(ByVal path As String, ByVal ThongSo As String, ByVal GiaTriMacDinh As String) As String
        Dim lstStr As List(Of String)
        Try
            Dim kq As String = GiaTriMacDinh
            lstStr = IO.File.ReadAllText(path).Split(vbCrLf).ToList()

            For Each sotrang In lstStr
                sotrang = sotrang.Trim

                If sotrang.Split(" ")(0) = ThongSo Then
                    kq = sotrang.Split(" ")(1)
                End If

            Next

            Return kq
        Catch ex As Exception
            Return GiaTriMacDinh
        End Try
    End Function

    Public Shared Sub GhiThongSo(ByVal path As String, ByVal ThongSo As String, ByVal GiaTri As String)
        Dim lststr As New List(Of String)
        Try
            Dim rd As New IO.StreamReader(path)

            While rd.Peek >= 0
                lststr.Add(rd.ReadLine)
            End While

            Dim ok As Boolean = False
            For i = 0 To lststr.Count - 1
                Dim s As String = lststr(i).Trim

                If s.Split(" ")(0) = ThongSo Then
                    lststr(i) = ThongSo + " " + GiaTri
                    ok = True
                    Exit For
                End If
            Next

            If Not (ok) Then
                lststr.Add(ThongSo + " " + GiaTri)
            End If

            rd.Close()

            Dim wt As New IO.StreamWriter(path)
            For Each s In lststr
                wt.WriteLine(s)
            Next
            wt.Close()
        Catch ex As Exception

        End Try
    End Sub
End Class
